package com.sky.service.admin.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.RedisConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.admin.CategoryMapper;
import com.sky.mapper.admin.DishMapper;
import com.sky.mapper.admin.SetmealMapper;
import com.sky.pojo.Category;
import com.sky.result.PageResult;
import com.sky.service.admin.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Locale;

/**
 * @Description CategoryServiceImpl
 * @Author lyh
 * @Date 2024-09-20
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 根据条件进行分页查询
     * @param dto
     * @return
     */
    @Override
    public PageResult pageQuery(CategoryPageQueryDTO dto) {
        //开启分页插件
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        //根据条件进行分页查询
        Page<Category> page=categoryMapper.pageQuery(dto);
        //返回分页结果
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 新增分类
     * @param dto
     */
    @Override
    public void save(CategoryDTO dto) {
        //拷贝属性
        Category category = new Category();
        BeanUtils.copyProperties(dto,category);
        //补充属性
       /* category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());
        category.setCreateUser(BaseContext.getCurrentId());*/
        //分类状态默认为禁用状态0
        category.setStatus(StatusConstant.DISABLE);
        categoryMapper.save(category);
        //数据库中的数据发生改变，删除redis中的缓存
        redisTemplate.delete(RedisConstant.CATEGORY_CACHE_PREFIX+category.getType());
    }

    @Override
    public void delete(Integer id) {
        Category category = categoryMapper.getById(id);
        //查询当前分类是否关联了菜品，如果关联了就抛出业务异常
        Integer count = dishMapper.countByCategoryId(id);
        if(count>0){
            throw  new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
        //查询当前分类是否关联了套餐，如果关联了就抛出业务异常
        Integer integer = setmealMapper.countByCategoryId(id);
        if(integer>0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        categoryMapper.delete(id);
        //数据库中的数据发生改变，删除redis中的缓存
        redisTemplate.delete(RedisConstant.CATEGORY_CACHE_PREFIX+category.getType());
    }

    /**
     * 修改分类
     * @param dto
     */
    @Override
    public void update(CategoryDTO dto) {
        Category category = new Category();
        BeanUtils.copyProperties(dto,category);
        /*category.setUpdateUser(BaseContext.getCurrentId());
        category.setUpdateTime(LocalDateTime.now());*/
        categoryMapper.update(category);
        //数据库中的数据发生改变，删除redis中的缓存
        redisTemplate.delete(RedisConstant.CATEGORY_CACHE_PREFIX+category.getType());
    }
    /**
     * 启用禁用分类
     * @param status
     * @return
     */
    @Override
    public void updateStatus(Integer status, Long id) {
        Category category = categoryMapper.getById(Integer.parseInt(id.toString()));
        categoryMapper.updateStatus(status,id);
        //数据库中的数据发生改变，删除redis中的缓存
        redisTemplate.delete(RedisConstant.CATEGORY_CACHE_PREFIX+category.getType());
    }
    /**
     * 根据类型查询分类
     * @param type =1  菜品分类    type= 2  套餐分类
     * @return
     */
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public List<Category> list(Integer type) {
        //先从redis缓存中取数据
        List<Category> categoryList= (List<Category>) redisTemplate.opsForValue().get(RedisConstant.CATEGORY_CACHE_PREFIX+type);
        //没有从数据库中取，并加入redis缓存
        if(categoryList==null || categoryList.size()==0){
           categoryList= categoryMapper.list(type);
           redisTemplate.opsForValue().set(RedisConstant.CATEGORY_CACHE_PREFIX+type,categoryList);
            System.out.println("------------从数据库中获取数据------------");
        }else{
            System.out.println("------------从redis缓存中获取数据------------");
        }
        return categoryList;
    }
     //用户端接口
    /**
     *
     * @return
     */
    @Override
    public List<Category> list1() {
        return categoryMapper.list1();
    }
}
